package ant.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.Activity;
import ant.tools.PrintOut;

/**
 * 类简介:对本地文件进行操作,读取图片，文本，xml等
 * 
 * @author J.H
 * 
 */
public class LocationFile {

	/**
	 * 将指定路径的文件创建一个字符读取流InputStreamReader
	 * @param path  文件的绝对路径
	 * @return 字符读取流
	 * @throws FileNotFoundException 向外抛出可能发生文件找不到的异常
	 */
	public InputStreamReader getStreamReader(String path) throws FileNotFoundException {

		File file = new File(path);
		InputStreamReader inputStreamReader = null;
		FileInputStream fileInputStream = null;
		if (file.exists()) {
			/* 创建文件流 */
			fileInputStream = new FileInputStream(file);
			inputStreamReader = new InputStreamReader(fileInputStream);
		}

		return inputStreamReader;
	}

	/**
	 * 将字符流转换成字符串
	 * @return 返回从字符流中读取到的字符串
	 * @throws IOException  读取中能出现IO异常，向外抛出
	 */
	public String streamToString(InputStreamReader inputStreamReader) throws IOException{
		
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		StringBuffer buffer = new StringBuffer();
		String str = null;
		/*循环读取每一行的字符串直到为null*/
		while ((str = bufferedReader.readLine()) != null) {
			buffer.append(str);
		}
		inputStreamReader.close();
		return buffer.toString();
	}
	
	/**
	 * 将字符串保存到SD卡中.如果已经有文件,写入内容将覆盖上一次缓存的数据
	 * @param path  保存的路径
	 * @param fileName  保存的文件名称
	 * @param source  需要保存的字符串
	 */
	public void writeCache(String path,String fileName,String source){
		PrintOut.info("Framework FilePath", path+fileName);
		try {
			File filePath=new File(path);
			if(!filePath.exists()){
				filePath.mkdirs();
			}
			
			File file=new File(path+fileName);
			file.createNewFile();
			
			FileWriter fileWriter=new FileWriter(file);
			BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
			bufferedWriter.append(source);
			bufferedWriter.flush();
			
			bufferedWriter.close();
			fileWriter.close();
		} catch (IOException e) {
			PrintOut.error("Framework IOException","缓存写入失败,文件名:"+fileName);
			e.printStackTrace();
		}
	}
	
	/**
	 * 通过一个类的包名和类名生成SD卡缓存的路径,只需要在
	 * 返回的String(字符串)添加需要缓存的文件即可.
	 * @return 自动生成的路径
	 */
	public static String getSDPath(Activity activity){
		Class<? extends Activity> activeClass = activity.getClass();
		String className = activeClass.getSimpleName();
		String sdPath = activity.getCacheDir().getPath();
		
		StringBuilder builder=new StringBuilder();
		builder.append(sdPath);
		builder.append("/");
		builder.append(className);
		builder.append("/");
		return builder.toString();
	}
	
	/**
	 * 递归清除传入File类文件夹下的所有缓存,但不删除File自己
	 * @param file  需要删除的文件
	 */
	public void clearCache(File file){
		File[] listFiles = file.listFiles();
		if(listFiles==null){
			file.delete();
		}else{
			for (int i = 0; i < listFiles.length; i++) {
				clearCache(listFiles[i]);
				listFiles[i].delete();
			}
		}
	}
}
